package com.fuce.one.deploy.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


public class FileUtils {

    /**
     * 向指定文件写入内容(如文件存在，则先删除再创建写入)
     *
     * @throws IOException
     */
    public static boolean save(MultipartFile file, String tomcatName) throws IOException {
        try {

            String address = TomcatUtil.getSetting().getByGroup("address", tomcatName);
            if(StringUtils.isEmpty(address)){
                return false;
            }
            File toFile = null;
            InputStream ins = null;
            ins = file.getInputStream();
            if (file.getOriginalFilename().contains("/")) {
                String[] split = file.getOriginalFilename().split("/");
                String fileName = split[split.length - 1];
                toFile = new File(fileName);
            } else {
                toFile = new File(address+"\\"+file.getOriginalFilename());
            }

            deleteTomcatFile(toFile.getPath());



            OutputStream os = new FileOutputStream(toFile);
            int bytesRead = 0;
            byte[] buffer = new byte[1024000];
            while ((bytesRead = ins.read(buffer, 0, 1024000)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();

            ins.close();

            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }

    /**
     * 删除tomcat文件
     * @param warPath
     */
    private static void deleteTomcatFile(String warPath) {
        File file = new File(warPath);
        file.delete();

        delFolder(warPath.substring(0,warPath.length()-4));

    }

    /**
     * 删除文件夹
     * @param folderPath
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定文件夹下的所有文件
     * @param path
     * @return
     * @author fuce
     * @Date 2020年3月7日 下午5:56:03
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }



}